﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for SliderDA
/// </summary>
public class SliderDA
{
    Connection conn = new Connection();

    public List<Slider> getData() {
        try
        {
           
            List<Slider> objSlider = new List<Slider>();
            string sql = "Select * from QuangCao";
            SqlCommand cmd = new SqlCommand(sql, conn.getCon());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Slider s = new Slider(Convert.ToInt32( dt.Rows[i]["ID"].ToString()), dt.Rows[i]["url"].ToString(), dt.Rows[i]["link"].ToString(), dt.Rows[i]["noidung"].ToString());
                objSlider.Add(s);
            }
                
            
           
            return objSlider;
        }
        catch (Exception )
        {

            throw;
        }
        finally {
            conn.closeCon();
        }
    }
    public void updateAnh(int id, string imageName)
    {
        try
        {
            string str = "Update QuangCao set url=@image where id=@id";
            SqlCommand com = new SqlCommand(str, conn.getCon());
            com.Parameters.Add("@image", System.Data.SqlDbType.NVarChar, 50).Value = "~/Image/Slide/" + imageName;
            com.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
            conn.openCon();
            com.ExecuteNonQuery();
            conn.closeCon();
        }
        catch (Exception ex)
        {

            throw new Exception("Khong update duoc anh " + ex.Message);
        }
        finally {
            conn.closeCon();
        }
    }
}